<template>
  <div class="appointment-board">
    <!-- 顶部操作区域 -->
    <div
      style="
        margin-top: 15px;
        width: 300px;
        margin-left: 45px;
        margin-bottom: 15px;
      "
    >
      <el-input
        placeholder="项目名称"
        v-model="query"
        class="input-with-select"
        :clearable="true"
        @blur="reList()"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getDetailByName()"
        ></el-button>
      </el-input>
    </div>
    <!-- 表格部分 -->
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="service_detail_id"
        label="项目编号"
        width="100%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="service_type_id"
        label="项目类型"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ getBDescription(scope.row.service_type_id) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed
        prop="service_name"
        label="项目名称"
        width="100%"
        align="center"
      >
      </el-table-column>

      <el-table-column
        fixed
        prop="service_price"
        label="项目价格"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="service_description"
        label="项目描述"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="is_appointment_available"
        label="可预约"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.is_appointment_available == 1 ? "是" : "否"
          }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
          <el-button
            @click="changeClick(scope.row)"
            :type="scope.row.is_del == 0 ? 'danger' : 'success'"
            size="small"
            >{{ scope.row.is_del == 0 ? "下架" : "上架" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-sizes="[5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    ></el-pagination>
    <el-drawer
      title="服务详情"
      :visible.sync="drawer"
      :before-close="handleClose"
    >
      <div>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <div>{{ handle_obj.service_name }}</div>
          </div>
          <div class="text item">
            {{ "项目编号：" + handle_obj.service_detail_id }}
          </div>
          <!-- <div class="text item">{{ "项目描述：" + handle_obj.service_description }}</div>
          <div class="text item">{{ "项目价格：" + handle_obj.service_price+"元" }}</div> -->
          <el-form :model="handle_obj" label-width="100px">
            <el-form-item label="项目描述" required>
              <el-input
              type="textarea" :rows="3"
                v-model="handle_obj.service_description"
                placeholder="请输入项目描述"
              ></el-input>
            </el-form-item>
            <el-form-item label="项目价格" required>
              <el-input
                v-model="handle_obj.service_price"
                placeholder="请输入金额"
              ></el-input>
            </el-form-item>
            <el-button type="success" style="margin:auto 50% ;" @click="update()">提交修改</el-button>
          </el-form>
        </el-card>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  getAllServiceDetails,
  getServiceCategoties,
  changeStatus,
  updateServicedetail,
  getDetailByName
} from "@/utils/request";
export default {
  name: "DyymanagevueServiceDetailsPage",

  data() {
    return {
      query: "",
      drawer: false,
      tableData: [],
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页显示条数
      totalItems: 0, // 总记录数
      handle_obj: "", //操作对象
      type_data: [],
      show_data: [],
    };
  },

  mounted() {
    this.getList();
  },

  methods: {
    async getList() {
      await getAllServiceDetails()
        .then((res) => {
          this.tableData = res.data.data;
        })
        .catch((error) => {
          // 处理错误
          console.error(error);
        });
      await getServiceCategoties()
        .then((res) => {
          this.type_data = res.data.data;
        })
        .catch((error) => {
          // 处理错误
          console.error(error);
        });
    },
    reList() {
      this.getList();
    },
    editClick(operator) {
      this.drawer = true;
      this.handle_obj = operator;
    },
    handleClose(done) {
      //   this.$confirm("确认关闭？")
      //     .then((_) => {

      //     })
      //     .catch((_) => {});
      done();
      this.handle_obj = "";
    },
    update(){
      updateServicedetail(this.handle_obj).then((res)=>{
        this.reList()
      })
    },
    // 分页器页码变化事件处理函数
    handleCurrentChange(newPage) {
      this.currentPage = newPage; // 更新当前页码
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize; //更新每页数据条数
    },
    reList() {
      this.getList();
    },
    changeClick(o) {
      changeStatus(o.service_detail_id).then((res) => {
        this.reList();
      });
    },
    getDetailByName(){
      if (this.query != "") {
        getDetailByName(this.query)
          .then((res) => {
            this.tableData = res.data.data;
          })
          .catch((err) => {});
      }
    },
    getBDescription(aId) {
      const matchedB = this.type_data.find((b) => b.service_id === aId);
      return matchedB ? matchedB.service_name : "No match found";
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  position: absolute;
  bottom: 0;
  left: 45%;
  /* width: 30%; */
  bottom: 5%;
  background-color: #fff;
  z-index: 999; /* 可以根据需要设置z-index值，确保分页器显示在其他内容上方 */
  box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.1); /* 添加阴影效果（可选） */
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
</style>